﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace XP.Utilities.Utility
{
    public class XmlSerializer
    {
        #region 字符串的序列化和反序列化
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string SerializeToString<T>(T t)
        {
            using (StringWriter sw = new StringWriter())
            {
                System.Xml.Serialization.XmlSerializer xz =
                    new System.Xml.Serialization.XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                return sw.ToString();
            }
        }

        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="s">对象序列化后的Xml字符串</param>
        /// <returns></returns>
        public static T DeserializeFromString<T>(string s)
        {
            using (StringReader sr = new StringReader(s))
            {
                System.Xml.Serialization.XmlSerializer serializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(T));
                return (T)serializer.Deserialize(sr);
            }
        }
        #endregion

        #region 文件的序列化和反序列化
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static void SerializeToFile<T>(T t, string fileName)
        {
            using (TextWriter tw = new StreamWriter(fileName))
            {
                System.Xml.Serialization.XmlSerializer xz =
                    new System.Xml.Serialization.XmlSerializer(t.GetType());
                xz.Serialize(tw, t);
                tw.Close();
            }
        }

        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static T DeserializeFromFile<T>(string fileName)
        {
            using (FileStream fs = new FileStream(fileName, FileMode.Open))
            {
                System.Xml.Serialization.XmlSerializer serializer =
                    new System.Xml.Serialization.XmlSerializer(typeof(T));
                T t = (T)serializer.Deserialize(fs);
                fs.Close();

                return t;
            }
        }
        #endregion

        #region 二进制的序列化和反序列化
        ///  <summary>  
        ///  序列化为二进制字节数组  
        ///  </summary>  
        ///  <param  name="request">要序列化的对象 </param>  
        ///  <returns>字节数组 </returns>  
        public static string SerializeToBinary<T>(T o)
        {
            StringBuilder xml = new StringBuilder();
            using (System.IO.MemoryStream memStream = new System.IO.MemoryStream())
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter serializer = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                serializer.Serialize(memStream, o);
                byte[] buffer = memStream.ToArray();
                for (int i = 0; i < buffer.Length; i++)
                {
                    xml.Append(buffer[i].ToString("X").PadLeft(2, '0'));
                }
            }
            return xml.ToString();
        }
        ///  <summary>  
        ///  从二进制数组反序列化得到对象  
        ///  </summary>  
        ///  <param  name="buf">字节数组 </param>  
        ///  <returns>得到的对象 </returns>  
        public static T DeserializeBinary<T>(string s)
        {
            byte[] buffer = new byte[s.Length / 2];
            for (int i = 0; i < s.Length; i += 2)
            {
                buffer[i / 2] = Convert.ToByte(s.Substring(i, 2), 16);
            }
            T o;
            using (System.IO.MemoryStream memStream = new MemoryStream(buffer))
            {
                memStream.Position = 0;
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter deserializer =
                    new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                o = (T)deserializer.Deserialize(memStream);
                memStream.Close();
            }
            return o;
        }
        #endregion
    }
}
